package com.jwaoo.chat.session.actions;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;

import com.dx.as.common.CommonUtils;
import com.jwaoo.chat.ChatConstants;
import com.jwaoo.chat.Message;
import com.jwaoo.chat.R;
import com.jwaoo.chat.attachment.FileAttachment;
import com.jwaoo.chat.data.db.MessageHandler;
import com.jwaoo.media.video.helper.VideoHelper;
import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by Jwaoo on 2017/7/7.
 */
public class VideoAction extends BaseAction {
    private VideoHelper videoHelper;
    private int snap;
    public VideoAction() {
        super(R.drawable.dialogue_more_camera_btn, R.string.input_panel_video);
    }
    private VideoHelper getVideoHelper(){
        if (videoHelper == null) videoHelper = new VideoHelper(getActivity(),listener);
        return videoHelper;
    }

    @Override
    public void onClick() {
        snap = ChatConstants.unsnap;
        getVideoHelper().captureVideo();
    }

    public void chooseVideoFromCamera(){
        snap = ChatConstants.snap;
        getVideoHelper().chooseVideoFromCamera();
    }

    public void chooseVideoFromLocal(){
        snap = ChatConstants.snap;
        getVideoHelper().chooseVideoFromLocal();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        getVideoHelper().onActivityResult(requestCode,resultCode,data);
    }

    private VideoHelper.ICaptureVideoSuccessListener listener = new VideoHelper.ICaptureVideoSuccessListener() {
        @Override
        public void onOnSuccess(long duration, int height, int width, File file, String md5) {
            try{
                String bitmapPath = CommonUtils.getImgDir(getActivity().getApplicationContext()) + md5;
                Bitmap bitmap = getVideoThumbnail(file.getAbsolutePath());
                saveBitmap(bitmap,bitmapPath);
                FileAttachment fileAttachment = new FileAttachment();
                fileAttachment.setPath(file.getAbsolutePath());
                fileAttachment.setDuration(duration);
                fileAttachment.setHeight(height);
                fileAttachment.setWidth(width);
                fileAttachment.setThumbnail(bitmapPath);
                fileAttachment.setAttachstate(ChatConstants.transferring);
                Message message = MessageHandler.getInstace(getActivity()).createMessage(ChatConstants.video,getContainer().account,fileAttachment);
                message.setSnap(snap);
                sendMessage(message);
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    };

    private void saveBitmap(Bitmap mBitmap,String path) throws Exception{
        File file = new File(path);
        FileOutputStream out = new FileOutputStream(file);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
//            //保存图片后发送广播通知更新数据库
//            Uri uri = Uri.fromFile(file);
//            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
    }

    public Bitmap getVideoThumbnail(String videoPath) {
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(videoPath);
        Bitmap bitmap = media.getFrameAtTime();
        return bitmap;
    }
}
